package com.example.weblending.controlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.Post;
import com.example.weblending.domain.User;
import com.example.weblending.service.PostService;
import com.example.weblending.service.UserService;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/lendng-product")
@CrossOrigin
public class LendingProductController {
    @Resource
    private UserService userService;
    @Resource(name = "lengdingProductServiceImpl")
    private PostService postService;

    /**
     * 获得已经上架的产品列表
     */
    @GetMapping("{page}/{limit}")
    public Result show(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        Page<Post> conditionPage =postService.getShow(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 获得某一产品的详细信息
     */
    @GetMapping("{id}")
    public Result show(@PathVariable String id, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        Post post=postService.getById(id);
        return Result.ok().data("item",post);
    }

    /**
     * 获得自己创建的产品列表
     */
    @GetMapping("list/{page}/{limit}")
    public Result list(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user= JwtUtils.checkUser(request,userService);
        Page<Post> conditionPage =postService.getList(page,limit,user);
        return Result.ok().data(conditionPage).data("item",conditionPage.getRecords());
    }

    /**
     * 管理员新建产品
     */
    @PostMapping("")
    public Result create(@RequestBody Post post, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success= postService.create(post,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员上架产品
     */
    //增加清洗次数
    @PostMapping("/pass/{id}")
    public Result pass(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=postService.setState(id,1,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员下架产品
     */
    @PostMapping("deny/{id}")
    public Result deny(@PathVariable int id,HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        boolean success=postService.setState(id,-1,user);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员删除指定自己创建的产品
     */
    @DeleteMapping("")
    public Result delete(@RequestBody int[] list, HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        boolean success = postService.delete(list,user);
        return success?Result.ok():Result.error();
    }
}
